#!/usr/bin/env bash
#
# Arch Linux autoinstaller minimal
# --------------------------------------------------
# Author    : iknski
# License   : The MIT License (https://opensource.org/license/mit/)
# Version   : v 1.0
# --------------------------------------------------
# 22.04.2023

clear

loadkeys ru
setfont cyr-sun16
#-------------------------------------------------------------------------------
echo "
____ ____ ____ ____ ____________ ____ ____ ____ ____ ____ ____ ____
||a |||r |||c |||h |||   from   |||i |||n |||s |||t |||a |||l |||l ||
||__|||__|||__|||__|||__iknski__|||__|||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__________\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|

WARNING!!!
This script is fully automated installer for Arch Linux on your PC
Minimal version
Is only for UEFI API and SSD
If you have a BIOS, please do everything manually
Also you must have internet
"
sleep 3
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
echo -en "Select the disk where Arch Linux will be installed\n"
echo -en "--------------------------------------------------\n"
lsblk -o NAME,LABEL,UUID,FSTYPE,FSAVAIL,MOUNTPOINTS
echo -en "--------------------------------------------------\n"
echo -en "Please enter the name of disk (like sda, nvme0n1)\n"
echo -en "--------------------------------------------------\n"
echo -en "\n"
#-------------------------------------------------------------------------------
blocks=()
for item in $(ls /sys/class/block)
do
blocks+=( "$item" )
done
#
for item in "${blocks[@]}"
do
read -r -p "| Arch Linux will be installed to -> /dev/. >>> " archdisk
	if [[ "${blocks[*]}" =~ "$archdisk" ]]; then
		echo "Your choise: $archdisk"
		break
	else
		echo "$archdisk !!! incorrect input, try again ..."
		continue
	fi
done
#-------------------------------------------------------------------------------
user_answer=""
while true
do
	echo -en "--------------------------------------------------\n"
    echo -en "Create the partitions:\n"
    echo -en "**********************\n"
    echo -en "ESP (EFI system partition)\n"
    echo -en "| Type: EFI System (EF00)\n"
    echo -en "| Size: ~100M\n"
    echo -en "--------------------------------------------------\n"
    echo -en "BOOT\n"
    echo -en "| Type: Linux extended boot (EA00)\n"
    echo -en "| Size: At your discretion ~300M or more\n"
    echo -en "--------------------------------------------------\n"
    echo -en "ROOT\n"
    echo -en "| Type: Linux filesystem (8300)\n"
    echo -en "| Size: All other space\n"
    echo -en "--------------------------------------------------\n"
    echo -en "WARNING!!! all data on /dev/$archdisk will be REMOVED\n"
    echo -en "The answer \"No\" will terminate the script execution\n"
    echo -en "Continue?\n"
    echo -en "--------------------------------------------------\n"
    read -r -p "| Yes/No (Y/N) >>> " user_answer
	case $user_answer in
	    Yes | yes | Yep | yep | Y | y) echo "Preparing the disk ..."
	    sleep 2
	    wipefs -af /dev/$archdisk
		gdisk /dev/$archdisk
	    break;;
	    No | no | Nope | nope | N | n) echo "The script is closed ..."
	    sleep 2
	    clear
	    exit;;
	    *) echo "Incorrect input. Try again ..." continue;;
    esac
done
clear
#-------------------------------------------------------------------------------
echo -en "==================================================\n"
echo -en ">>>          disk partition selection          <<<\n"
echo -en "==================================================\n"
echo "
Please enter the disk partitions you want to
create filesystem ( like sda1, sdb2, nvme0n1p3 )

ATTENTION!!!
if you didn't create a swap partition, skip swap
"
echo -en "--------------------------------------------------\n"
lsblk -o NAME,LABEL,UUID,FSTYPE,FSAVAIL,MOUNTPOINTS
echo -en "--------------------------------------------------\n"
echo -en "\n"
echo -en "--------------------------------------------------\n"
read -r -p "| esp -> /dev/. >>> " esp
read -r -p "| boot -> /dev/. >>> " boot
read -r -p "| swap -> /dev/. >>> " swap
read -r -p "| root -> /dev/. >>> " root
echo -en "--------------------------------------------------\n"
echo -en "Your choise:\n"
echo -en "--------------------------------------------------\n"
echo -en "/dev/$esp ->  EFI system partition\n"
echo -en "/dev/$boot -> BOOT partition\n"
if [ -z "$swap" ]; then
    echo -en "/without a SWAP partition\n"
else
    echo -en "/dev/$swap -> SWAP partition\n"
fi
echo -en "/dev/$root -> ROOT partition\n"
echo -en "--------------------------------------------------\n"
echo -en "\n"
echo -en "--------------------------------------------------\n"
echo -en "If you want to add any additional disk, enter the name of the partition ( like sdb1 )\nor leave this field empty ...\n"
echo -en "--------------------------------------------------\n"
read -r -p "| your any disk -> /dev/. >>> " anydisk
echo -en "--------------------------------------------------\n"
echo -en "Enter the name of the directory where to mount it ( like storage, media, projects )\nor leave this field empty ...\n"
echo -en "--------------------------------------------------\n"
read -r -p "| create directory -> /. >>> " adddir
#-------------------------------------------------------------------------------
mkfs.vfat -F32 /dev/$esp
mkfs.vfat -F32 /dev/$boot
fatlabel /dev/$esp ESP
fatlabel /dev/$boot BOOT
if [ -n "$swap" ]; then
    mkswap /dev/$swap
    swapon /dev/$swap
fi
clear
#
while true
do
    echo -en "Select the file system you want to format the root partition\n"
    echo -en "1 - BTRFS\n2 - EXT4\n"
    read -r -p "| Your choise: >>> " fs_type
    case $fs_type in
        1) echo "File system selected - BTRFS"
        mkfs.btrfs -f /dev/$root
        btrfs filesystem label /dev/$root ROOT
		read -r -p "select the \"zstd\" compression level ( from 1 to 15 ) >>> " clvl
		mount  /dev/$root /mnt
		btrfs subvolume create /mnt/@
		btrfs subvolume create /mnt/@home
		btrfs subvolume create /mnt/@cache
		btrfs subvolume create /mnt/@log
		btrfs subvolume create /mnt/@snapshots
		umount -R /mnt
		mount -o rw,noatime,compress=zstd:$clvl,space_cache=v2,discard=async,ssd,ssd_spread,max_inline=256,subvol=@ /dev/$root /mnt
		mkdir -p /mnt/{home,.snapshots,var/{cache,log},$adddir}
		echo -en "--------------------------------------------------\n"
		ls -la --color /mnt
		echo -en "--------------------------------------------------\n"
		read -r -sn1 -p "check created dirs and press any key to continue..."; echo
		mount -o rw,noatime,compress=zstd:$clvl,space_cache=v2,discard=async,ssd,ssd_spread,max_inline=256,subvol=@home  /dev/$root  /mnt/home
		mount -o rw,noatime,compress=zstd:$clvl,space_cache=v2,discard=async,ssd,ssd_spread,max_inline=256,subvol=@cache  /dev/$root  /mnt/var/cache
		mount -o rw,noatime,compress=zstd:$clvl,space_cache=v2,discard=async,ssd,ssd_spread,max_inline=256,subvol=@log  /dev/$root  /mnt/var/log
		mount -o rw,noatime,compress=zstd:$clvl,space_cache=v2,discard=async,ssd,ssd_spread,max_inline=256,subvol=@snapshots  /dev/$root  /mnt/.snapshots
        break;;
        2) echo "File system selected - EXT4"
        mkfs.ext4 -F /dev/$root
        e2label /dev/$root ROOT
        mount  /dev/$root /mnt
        mkdir -p /mnt/{home,$adddir}
        break;;
        *) echo "Incorrect input. Try again ..." continue;;
    esac
done
#
#-------------------------------------------------------------------------------
while true
do
    echo -en "--------------------------------------------------\n"
    echo -en "Select the bootloader\n"
    echo -en "--------------------------------------------------\n"
    echo -en "| 1 - GRUB\n| 2 - SYSTEMD-BOOT\n"
    read -r -p "| Your choise (1/2): >>> " boot_type
    case $boot_type in
        1) echo "Bootloader selected - GRUB"
        mkdir -p /mnt/{boot,efi}
        mount /dev/$esp /mnt/efi
        mount /dev/$boot /mnt/boot
        break;;
        2) echo "Bootloader selected - SYSTEMD-BOOT"
        mkdir -p /mnt/{boot,efi}
        mount /dev/$esp /mnt/efi
        mount /dev/$boot /mnt/boot
        break;;
        *) echo "Incorrect input. Try again ..." continue;;
    esac
done
#-------------------------------------------------------------------------------
if [ -n "$anydisk" ]; then
    mount /dev/$anydisk /mnt/$adddir
fi
#
echo -en "--------------------------------------------------\n"
df -h
echo -en "--------------------------------------------------\n"
lsblk -o NAME,LABEL,UUID,FSTYPE,FSAVAIL,MOUNTPOINTS
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "partitions have been created, press any key to continue
"; echo
clear
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
echo -en "Create a hostname (your PC name)\n"
echo -en "--------------------------------------------------\n"
read -r -p "| hostname >>> " hostnm
echo -en "--------------------------------------------------\n"
echo -en "Create a password for the root user\n"
echo -en "--------------------------------------------------\n"
echo -en "WARNING!!! Remember or write this password\n"
gen_choice=""
again_choise=""
password=""
keygen() {
    while [[ "${#password}" -ne "$pass_len" ]]
        do
            rand_char=$(shuf -e -n 1 {A..Z} {a..z} {0..9})
            password+="$rand_char"
        done
}
echo -en "--------------------------------------------------\n"
echo -en "| 1 - Generate new random password\n"
echo -en "| 2 - Enter your password\n"
echo -en "--------------------------------------------------\n"
read -r -p "| Your choice (1/2): >>> " gen_choice
while true
do
	case $gen_choice in
	    1) password=""; echo -en "\n| Generating password...\n"
	    read -r -p "| Enter the length of password (up 8 to 16) >>> " pass_len
        keygen
        echo -en "\n"
        echo -en "**************************************************\n"
        echo -en "* Your password is: $password\n"
        echo -en "**************************************************\n"
        echo -en "\n"
        read -r -p "| Generate again? (Yes/No) >>> " again_choise
        case $again_choise in
            Yes | yes | Yep | yep | Y | y) continue;;
            No | no | Nope | nope | N | n) break;;
            *) echo -en "| Incorrect input, try again ...\n" continue;;
        esac;;
	    2) echo -en "\n"; read -r -p "| Enter your password here >>> " password
	    echo -en "\n"
        echo -en "**************************************************\n"
        echo -en "* Your password is: $password\n"
        echo -en "**************************************************\n"
        echo -en "\n"
	    break;;
	    *) echo -en "| Incorrect input, try again ...\n";;
    esac
done
rootpsw="$password"
echo -en "\n"
echo -en "--------------------------------------------------\n"
echo -en "| Password for root user applied\n"
echo -en "--------------------------------------------------\n"
echo -en "Create a user\n"
echo -en "--------------------------------------------------\n"
read -r -p "| username >>> " usernm
echo -en "--------------------------------------------------\n"
echo -en "Create a password for the $usernm\n"
echo -en "--------------------------------------------------\n"
echo -en "WARNING!!! Remember or write this password\n"
gen_choice=""
again_choise=""
password=""
echo -en "--------------------------------------------------\n"
echo -en "| 1 - Generate new random password\n"
echo -en "| 2 - Enter your password\n"
echo -en "--------------------------------------------------\n"
read -r -p "| Your choice (1/2) >>> " gen_choice
while true
do
	case $gen_choice in
	    1) password=""; echo -en "\n| Generating password...\n"
	    read -r -p "| Enter the length of password (up 8 to 16) >>> " pass_len
        keygen
        echo -en "\n"
        echo -en "**************************************************\n"
        echo -en "* Your password is: $password\n"
        echo -en "**************************************************\n"
        echo -en "\n"
        read -r -p "| Generate again? (Yes/No) >>> " again_choise
        case $again_choise in
            Yes | yes | Yep | yep | Y | y) continue;;
            No | no | Nope | nope | N | n) break;;
            *) echo -en "| Incorrect input, try again ...\n" continue;;
        esac;;
	    2) echo -en "\n"; read -r -p "| Enter your password here >>> " password
	    echo -en "\n"
        echo -en "**************************************************\n"
        echo -en "* Your password is: $password\n"
        echo -en "**************************************************\n"
        echo -en "\n"
	    break;;
	    *) echo -en "| Incorrect input, try again ...\n";;
    esac
done
userpsw="$password"
gen_choice=""
again_choise=""
password=""
echo -en "\n"
echo -en "--------------------------------------------------\n"
echo -en "Password for $usernm applied\n"
clear
#-------------------------------------------------------------------------------
echo -en "==================================================\n"
echo -en ">>>          Setting up a time zone            <<<\n"
echo -en "==================================================\n"
echo -en "\n"
echo -en "--------------------------------------------------\n"
echo -en "Choose your area (Europe, America, Africa, Asia ...)\n"
echo -en "--------------------------------------------------\n"
read -r -p "| your area >>> " area
echo -en "--------------------------------------------------\n"
echo -en "Choose your location (Moscow, Chicago, Cairo, Tokyo ...)\n"
echo -en "--------------------------------------------------\n"
read -r -p "| your city >>> " city
while true
do
echo -en "--------------------------------------------------\n"
echo -en "Choose your CPU vendor (amd or intel ...)\n"
echo -en "WARNING!!! enter in lower case ...\n"
echo -en "--------------------------------------------------\n"
read -r -p "| your CPU vendor >>> " cpu
vendors=("amd" "intel")
	if [[ "${vendors[*]}" =~ "$cpu" ]]; then
		echo "Your CPU vendor: $cpu"
		break
	else
		echo "$cpu !!! incorrect input, try again ..."
        continue
	fi
done
#-------------------------------------------------------------------------------
while true
do
    echo -en "--------------------------------------------------\n"
    echo -en "Choose a tool to create initramfs\n"
    echo -en "--------------------------------------------------\n"
    echo -en "1 - mkinitcpio\n2 - dracut\n"
    read -r -p "| Your choise: >>> " tool_type
    case $tool_type in
        1) echo "Tool selected - mkinitcpio"
        break;;
        2) echo "Tool selected - dracut"
        break;;
        *) echo "Incorrect input. Try again ..." continue;;
    esac
done
clear
#-------------------------------------------------------------------------------
driver_install="pacstrap -K /mnt dkms linux-headers $cpu-ucode --noconfirm"
boot_tools="pacstrap -K /mnt efibootmgr efifs efitools efivar --noconfirm"
pkgs_install="pacstrap -K /mnt bash-completion archlinux-contrib pacman-contrib pkgfile --noconfirm"
cmd_install="pacstrap -K /mnt bat broot duf exa glances gptfdisk fzf git htop neofetch openssh ranger wget whois --noconfirm"
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
echo -en "One monent please, the setup is in progress...\n"
echo -en "--------------------------------------------------\n"
timedatectl set-ntp true
timedatectl set-timezone $area/$city
timedatectl status
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Enables and starts the time network synchronization service...
System time zone set...
press any key to continue
--------------------------------------------------
"; echo
clear
#-------------------------------------------------------------------------------
pacman -Sy --noconfirm archlinux-keyring
pacman -S reflector --noconfirm
reflector --verbose --country 'Russia,Germany,' --latest 5 --sort rate --save /etc/pacman.d/mirrorlist
pacman -Syy
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "The latest mirror list from the Arch Linux Mirror Status applied...
press any key to continue ...
--------------------------------------------------
"; echo
clear
#-------------------------------------------------------------------------------
cp_net_settings=""
while true
do
	echo -en "--------------------------------------------------\n"
    echo -en "Do you want to copy the internet settings to the system being installed?\n"
    echo -en "--------------------------------------------------\n"
    read -r -p "| Yes/No (Y/N) >>> " cp_net_settings
	case $cp_net_settings in
	    Yes | yes | Yep | yep | Y | y) echo "Copying settings ..."
        mkdir -p /mnt/etc/systemd/network
        mkdir -p /mnt/var/lib/iwd
        mkdir -p /mnt/etc/iwd
        touch /mnt/etc/iwd/main.conf
        cp -a -T /var/lib/iwd/. /mnt/var/lib/iwd
        cp -a -T /etc/systemd/network/. /mnt/etc/systemd/network
	    break;;
	    No | no | Nope | nope | N | n) echo "Continue without copying ..."
	    break;;
	    *) echo "Incorrect input. Try again ..." continue;;
    esac
done
#-------------------------------------------------------------------------------
mkdir -p /mnt/etc/X11/xorg.conf.d
touch /mnt/etc/X11/xorg.conf.d/00-keyboard.conf
#-------------------------------------------------------------------------------
if [[ "$tool_type" = "1" ]]; then
    echo -en "--------------------------------------------------\n"
    echo -en "Starting Arch Linux installation\n"
    echo -en "Before installing, select the tool to create initramfs that you indicated earlier!!!\n"
    echo -en "--------------------------------------------------\n"
    pacstrap -Ki /mnt base base-devel linux linux-firmware reflector rsync micro
    ${driver_install}
    echo -en "--------------------------------------------------\n"
    read -r -sn1 -p "The base Arch Linux packages have been installed...
press any key to apply system settings
--------------------------------------------------
"; echo
else
    echo -en "--------------------------------------------------\n"
    echo -en "Starting Arch Linux installation\n"
    echo -en "Before installing, select the tool to create initramfs that you indicated earlier!!!\n"
    echo -en "--------------------------------------------------\n"
    #---------------------------------------------------------------------------
    mkdir -p /mnt/usr/local/bin
    mkdir -p /mnt/etc/pacman.d/hooks
    touch /mnt/usr/local/bin/dracut-install.sh
    touch /mnt/usr/local/bin/dracut-remove.sh
    touch /mnt/etc/pacman.d/hooks/90-dracut-install.hook
    touch /mnt/etc/pacman.d/hooks/60-dracut-remove.hook
    #---------------------------------------------------------------------------
    {
        echo "#!/usr/bin/env bash"
        echo ""
        echo "args=('--force' '--no-hostonly-cmdline')"
        echo ""
        echo "while read -r line; do"
        echo "    if [[ \"\$line\" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then"
        echo "        read -r pkgbase < \"/\${line}\""
        echo "        kver=\"\${line#'usr/lib/modules/'}\""
        echo "        kver=\"\${kver%'/pkgbase'}\""
        echo ""
        echo "        install -Dm0644 \"/\${line%'/pkgbase'}/vmlinuz\" \"/boot/vmlinuz-\${pkgbase}\""
        echo "        dracut \"\${args[@]}\" --hostonly \"/boot/initramfs-\${pkgbase}.img\" --kver \"\$kver\""
        echo "        dracut \"\${args[@]}\" --no-hostonly \"/boot/initramfs-\${pkgbase}-fallback.img\" --kver \"\$kver\""
        echo "    fi"
        echo "done"
    } >> /mnt/usr/local/bin/dracut-install.sh
    #---------------------------------------------------------------------------
    {
        echo "#!/usr/bin/env bash"
        echo ""
        echo "while read -r line; do"
        echo "    if [[ \"\$line\" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then"
        echo "        read -r pkgbase < \"/\${line}\""
        echo "        rm -f \"/boot/vmlinuz-\${pkgbase}\" \"/boot/initramfs-\${pkgbase}.img\" \"/boot/initramfs-\${pkgbase}-fallback.img\""
        echo "    fi"
        echo "done"
    }  >> /mnt/usr/local/bin/dracut-remove.sh
    #---------------------------------------------------------------------------
    {
        echo "[Trigger]"
        echo "Type = Path"
        echo "Operation = Install"
        echo "Operation = Upgrade"
        echo "Target = usr/lib/modules/*/pkgbase"
        echo ""
        echo "[Action]"
        echo "Description = Updating linux initcpios (with dracut!)..."
        echo "When = PostTransaction"
        echo "Exec = /usr/local/bin/dracut-install.sh"
        echo "Depends = dracut"
        echo "NeedsTargets"
    } >> /mnt/etc/pacman.d/hooks/90-dracut-install.hook
    #---------------------------------------------------------------------------
    {
        echo "[Trigger]"
        echo "Type = Path"
        echo "Operation = Remove"
        echo "Target = usr/lib/modules/*/pkgbase"
        echo ""
        echo "[Action]"
        echo "Description = Removing linux initcpios..."
        echo "When = PreTransaction"
        echo "Exec = /usr/local/bin/dracut-remove.sh"
        echo "NeedsTargets"
    } >> /mnt/etc/pacman.d/hooks/60-dracut-remove.hook
    #---------------------------------------------------------------------------
    chmod +x /mnt/usr/local/bin/dracut-install.sh
    chmod +x /mnt/usr/local/bin/dracut-remove.sh
    chmod +x /mnt/etc/pacman.d/hooks/90-dracut-install.hook
    chmod +x /mnt/etc/pacman.d/hooks/60-dracut-remove.hook
    #---------------------------------------------------------------------------
    pacstrap -Ki /mnt base base-devel reflector rsync git openssh micro
    ${driver_install}
    arch-chroot /mnt /bin/bash -c "pacman -S linux linux-firmware"
    echo -en "--------------------------------------------------\n"
    read -r -sn1 -p "The base Arch Linux packages have been installed...
press any key to apply system settings
--------------------------------------------------
"; echo
fi
clear
#-------------------------------------------------------------------------------
genfstab -U /mnt >> /mnt/etc/fstab
arch-chroot /mnt /bin/bash -c "cat /etc/fstab"
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Filesystem table generated, press any key to continue
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
arch-chroot /mnt /bin/bash -c "sed -i s/'#en_US.UTF-8'/'en_US.UTF-8'/g /etc/locale.gen"
arch-chroot /mnt /bin/bash -c "sed -i s/'#ru_RU.UTF-8'/'ru_RU.UTF-8'/g /etc/locale.gen"
arch-chroot /mnt /bin/bash -c "locale-gen"
arch-chroot /mnt /bin/bash -c "echo 'LANG=ru_RU.UTF-8' > /etc/locale.conf"
{
    echo "KEYMAP=ru"
    echo "FONT=cyr-sun16"
} >> /mnt/etc/vconsole.conf
{
    echo "Section \"InputClass\""
    echo "    Identifier \"system-keyboard\""
    echo "    MatchIsKeyboard \"on\""
    echo "    Option \"XkbLayout\" \"us,ru\""
    echo "    Option \"XkbModel\" \"pc105\""
    echo "    Option \"XkbOptions\" \"grp:ctrl_shift_toggle,terminate:ctrl_alt_bksp\""
    echo "EndSection"
} >> /mnt/etc/X11/xorg.conf.d/00-keyboard.conf
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Locales generated, press any key to continue
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
arch-chroot /mnt /bin/bash -c "ln -sf /usr/share/zoneinfo/$area/$city /etc/localtime"
arch-chroot /mnt /bin/bash -c "hwclock --systohc"
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Time is synchronized, press any key to continue
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
arch-chroot /mnt /bin/bash -c "echo '$hostnm' > /etc/hostname"
echo -en "--------------------------------------------------\n"
arch-chroot /mnt /bin/bash -c "cat /etc/hostname"
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Hostname applied, press any key to continue
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
{
    echo "127.0.0.1	localhost"
    echo "::1		localhost"
    echo "127.0.1.1	$hostnm.localdomain	$hostnm"
} >> /mnt/etc/hosts
echo -en "--------------------------------------------------
"
arch-chroot /mnt /bin/bash -c "cat /etc/hosts"
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Hosts settings applied, press any key to install bootloader, drivers, file system and network tools
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
if [[ "$fs_type" = "1" ]]; then
    pacstrap -K /mnt btrfs-progs --noconfirm
    if [[ "$tool_type" = "1" ]]; then
        arch-chroot /mnt /bin/bash -c "sed -i s/'MODULES=()'/'MODULES=(crc32c libcrc32c zlib_deflate btrfs)'/g /etc/mkinitcpio.conf"
    fi
else
    pacstrap -K /mnt e2fsprogs --noconfirm
fi
#-------------------------------------------------------------------------------
${pkgs_install}
${cmd_install}
#-------------------------------------------------------------------------------
echo "root:$rootpsw" | arch-chroot /mnt chpasswd
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "ROOT user configured, press any key to continue
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
arch-chroot /mnt /bin/bash -c "useradd -m -G wheel -s /bin/bash $usernm"
arch-chroot /mnt /bin/bash -c "export EDITOR=micro"
echo "$usernm:$userpsw" | arch-chroot /mnt chpasswd
arch-chroot /mnt /bin/bash -c "sed -i s/'# %wheel ALL=(ALL:ALL) ALL'/'%wheel ALL=(ALL:ALL) ALL'/g /etc/sudoers"
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "$usernm created and configured, press any key to enabling and starting services
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
success=("Yes" "yes" "Yep" "yep" "Y" "y")
if [[ "${success[*]}" =~ "${cp_net_settings}" ]]; then
    pacstrap -K /mnt iwd --noconfirm
    {
        echo "[General]"
        echo "EnableNetworkConfiguration=true"
        echo ""
        echo "[Network]"
        echo "EnableIPv6=true"
        echo "RoutePriorityOffset=600"
    } >> /mnt/etc/iwd/main.conf
    arch-chroot /mnt /bin/bash -c "systemctl enable systemd-networkd.service"
    arch-chroot /mnt /bin/bash -c "systemctl enable systemd-resolved.service"
    arch-chroot /mnt /bin/bash -c "systemctl enable iwd.service"
else
    echo -en "WARNING!!! Internet settings were not copied\n"
    echo -en "Do not forget to install the necessary packages\n"
    echo -en "and configure the Internet before rebooting\n"
fi
#-------------------------------------------------------------------------------
arch-chroot /mnt /bin/bash -c "systemctl enable sshd.service"
arch-chroot /mnt /bin/bash -c "systemctl enable fstrim.timer"
arch-chroot /mnt /bin/bash -c "systemctl enable reflector.timer"
arch-chroot /mnt /bin/bash -c "systemctl enable systemd-timesyncd.service"
#-------------------------------------------------------------------------------
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Services are running
--------------------------------------------------
"; echo
#-------------------------------------------------------------------------------
sed -i s/'#Color'/'Color'/g /mnt/etc/pacman.conf
sed -i s/'#VerbosePkgLists'/'VerbosePkgLists'/g /mnt/etc/pacman.conf
sed -i s/'#ParallelDownloads = 5'/'ParallelDownloads = 5'/g /mnt/etc/pacman.conf
sed -i "/\[multilib\]/,/Include/"'s/^#//' /mnt/etc/pacman.conf
arch-chroot /mnt /bin/bash -c "pacman -Syy"
arch-chroot /mnt /bin/bash -c "reflector --verbose --country 'Russia,Germany,' --latest 5 --sort rate --save /etc/pacman.d/mirrorlist"
arch-chroot /mnt /bin/bash -c "sed -i s/'--country France,Germany'/'--country Russia,Germany'/g /etc/xdg/reflector/reflector.conf"
arch-chroot /mnt /bin/bash -c "sed -i s/'--sort age'/'--sort rate'/g /etc/xdg/reflector/reflector.conf"
echo -en "--------------------------------------------------\n"
read -r -sn1 -p "Pacman configured, press any key to install bootloader..."; echo
echo -en "\n"
#-------------------------------------------------------------------------------
# if [[ "$tool_type" = "1" ]]; then
#     arch-chroot /mnt /bin/bash -c "mkinitcpio -p linux"
# else
#     arch-chroot /mnt /bin/bash -c "dracut --force --hostonly --no-hostonly-cmdline /boot/initramfs-linux.img"
#     arch-chroot /mnt /bin/bash -c "dracut --force /boot/initramfs-linux-fallback.img"
# fi
#-------------------------------------------------------------------------------
# echo -en "--------------------------------------------------\n"
# read -r -sn1 -p "Initial ramdisk environment created, press any key to install bootloader
# --------------------------------------------------
# "; echo
#-------------------------------------------------------------------------------
${boot_tools}
if [[ "$boot_type" = "1" ]]; then
    pacstrap -K /mnt grub os-prober --noconfirm
    arch-chroot /mnt /bin/bash -c "grub-install --efi-directory=/efi --boot-directory=/boot --bootloader-id=\"Linux Boot Manager\""
    arch-chroot /mnt /bin/bash -c "grub-mkconfig -o /boot/grub/grub.cfg"
    read -r -sn1 -p "GRUB installed and configured
-------------------------------------------------------------------------------
All done! Arch Linux installed and ready to use!
Please << umount -a >> all devices and << reboot >> in to the new system!!!
-------------------------------------------------------------------------------
"; echo
else
    arch-chroot /mnt /bin/bash -c "bootctl --esp-path=/efi --boot-path=/boot install --efi-boot-option-description=\"Linux Boot Manager\""
    {
        echo "timeout menu-force"
        echo "console-mode auto"
        echo "editor no"
        echo "default arch.conf"
    } >> /mnt/efi/loader/loader.conf
    #---------------------------------------------------------------------------
    {
        echo "title    Arch Linux"
        echo "linux    /vmlinuz-linux"
        echo "initrd    /$cpu-ucode.img"
        echo "initrd    /initramfs-linux.img"
    } >> /mnt/boot/loader/entries/arch.conf
    #---------------------------------------------------------------------------
    {
        echo "title    Arch Linux fallback"
        echo "linux    /vmlinuz-linux"
        echo "initrd    /$cpu-ucode.img"
        echo "initrd    /initramfs-linux-fallback.img"
    } >> /mnt/boot/loader/entries/arch-fb.conf
    #---------------------------------------------------------------------------
    if [[ "$fs_type" = "1" ]]; then
        arch-chroot /mnt /bin/bash -c "echo 'options    rw root=\"LABEL=ROOT\" quiet splash loglevel=3 nowatchdog raid=noautodetect rootfstype=btrfs rootflags=subvol=@' >> /boot/loader/entries/arch.conf"
        arch-chroot /mnt /bin/bash -c "echo 'options    rw root=\"LABEL=ROOT\" quiet splash loglevel=3 nowatchdog raid=noautodetect rootfstype=btrfs rootflags=subvol=@' >> /boot/loader/entries/arch-fb.conf"
    else
        arch-chroot /mnt /bin/bash -c "echo 'options    rw root=\"LABEL=ROOT\" quiet splash loglevel=3 nowatchdog raid=noautodetect rootfstype=ext4' >> /boot/loader/entries/arch.conf"
        arch-chroot /mnt /bin/bash -c "echo 'options    rw root=\"LABEL=ROOT\" quiet splash loglevel=3 nowatchdog raid=noautodetect rootfstype=ext4' >> /boot/loader/entries/arch-fb.conf"
    fi
    read -r -sn1 -p "SYSTEMD-BOOT installed and configured
-------------------------------------------------------------------------------
All done! Arch Linux installed and ready to use!
Please << umount -a >> all devices and << reboot >> in to the new system!!!
-------------------------------------------------------------------------------
"; echo
fi
#-------------------------------------------------------------------------------
exit
